home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7079 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: fwrite help?
  5. Date: 18 Feb 1996 11:45:37 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4g73h1$1en@sparcserver.lrz-muenchen.de>
  9. References: <4fmaig$7of@news.global1.net> <harmon.824165113@pegasus.montclair.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. harmon@pegasus.montclair.edu (Derek Harmon) writes:
  13.  
  14. >youngrc@global1.net writes:
  15. >>What would the syntax for fwrite be to output 8 characters of 
  16. >>char line[10] to a file?  K&R doesn't write much about it. Thanks
  17. >>for the help.
  18.  
  19. >size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream); is the
  20. >prototype I have.  You can use line to devolve into a pointer towards the
  21. >first element in line, sizeof(char) for size insures compatibility on machines
  22. >where a character is more than one byte, and since you want 8 characters, set
  23. >n to 8.
  24.  
  25. If sizeof(char) != 1, it's not C, it's somethig different. sizeof(char)
  26. is 1 by definition. sizeof returns the size of it's operand in chars,
  27. so, as far as the C programming language is concerned, there is _no_
  28. chance for sizeof(char) to be anything but 1.
  29.  
  30. >: fwrite( line, sizeof(char), 8, fp);
  31.  
  32. >   Of course, when they are contiguous in an array, and you know the sizeof a
  33. >char is 1 byte, it's much more efficient to say you want to write an 8-byte
  34. >block of memory once, instead of a 1-byte block of memory eight times.
  35.  
  36. Nothing prevents the implementation of fwrite() to treat 
  37.  
  38.   fwrite(line, 1, 8, fp) 
  39.  
  40. as efficient as
  41.  
  42.   fwrite(line, 8, 1, fp)
  43.  
  44. The only neccessary difference is the value returned on successful
  45. completion of the operation.
  46.  
  47. Kurt
  48. --
  49. | Kurt Watzka                             Phone : +49-89-2180-6254
  50. | watzka@stat.uni-muenchen.de
  51. | ua302aa@sunmail.lrz-muenchen.de
  52.